//
//  ViewController.m
//  20301触摸与手势
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    BOOL bMove;
    CGPoint pointXY;//
}
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;

@property (weak, nonatomic) IBOutlet UIView *touchView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.label1.text=@"触摸开始";
    self.label2.text=[NSString stringWithFormat:@"数量：%i",touches.count];
    UITouch *touch=touches.anyObject;
    CGPoint p=[touch locationInView:self.view];
    self.label3.text=[NSString stringWithFormat:@"坐标：%@",NSStringFromCGPoint(p)];
    self.label4.text=[NSString stringWithFormat:@"连击次数：%i",touch.tapCount];
    
    
    
        // 1. 得到触摸点是否在moveView范围内
    bMove = CGRectContainsPoint(self.touchView.frame, p);
    
        if(bMove)
        {
            // 计算触摸点距离中心点的偏移
             pointXY = CGPointMake(p.x - self.touchView.center.x, p.y - self.touchView.center.y);
        }

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.label1.text=@"触摸移动";
//    UITouch *touch=touches.anyObject;
//    CGPoint p=[touch locationInView:self.view];
    
    //self.touchView.center=p;
    
    if(bMove)
            {
                UITouch *touch = [touches anyObject];
                CGPoint point = [touch locationInView:self.view];
        
                // 2. 移动它
                // 触摸点-偏移
                self.touchView.center = CGPointMake(point.x - pointXY.x, point.y - pointXY.y);
            }

}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //self.label1.text=@"触摸结束";
    bMove=NO;
}
@end
